package com.simen.marqueeview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * 垂直跑马灯
 * Created by zhangming on 2017/3/1.
 */
public class VerticalMarqueeView extends View {

    public final static int MARQUEE_MESSAGE = 0;
    public final static int NEXT_MESSAGE = 1;
    public final static int DEFAULTDISTANCE = 5;
    public final static int DEFAULTSPEED = 60;
    public final static int DEFAULTTEXTSIZE = 20;//默认文字大小

    private int realWidth = 0, realHeight = 0;

    private int distance = DEFAULTDISTANCE; //每个时间单位行进的距离
    private long speed = DEFAULTSPEED;
    private float footerLine = 0;
    private float scrollTop = 0;
    private int index = 0;
    private boolean pause = true;

    private int textColor = Color.BLACK;
    private Paint textPaint = null;
    private List<MessageText> messages = new ArrayList<>();

    Handler mHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == VerticalMarqueeView.MARQUEE_MESSAGE) {
                continueMarquee();
                if (!pause) {
                    mHandler.sendEmptyMessageDelayed(MARQUEE_MESSAGE, speed);
                }
            } else {
                resume(0);
            }
            return true;
        }
    });

    public VerticalMarqueeView(Context context) {
        this(context, null);
    }

    public VerticalMarqueeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerticalMarqueeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        if (textPaint == null) {
            textPaint = new Paint();
            setTextSize(DEFAULTTEXTSIZE);
            textPaint.setColor(textColor);
            textPaint.setAntiAlias(true);//去掉文字的锯齿
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.realWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        this.realHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        calculateFooter();
        setMeasuredDimension(this.realWidth, this.realHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String text = getText();
        if (!TextUtils.isEmpty(text)) {
            canvas.drawText(text, 0, footerLine + scrollTop, textPaint);
        }
    }

    public int getIndex() {
        return index;
    }

    public MessageText getCurrentMessage() {
        if (messages != null && !messages.isEmpty()) {
            if (index >= messages.size()) {
                return messages.get(0);
            }
            return messages.get(index);
        }
        return null;
    }

    private String getText() {
        if (messages != null && !messages.isEmpty()) {
            if (index >= messages.size()) {
                index = 0;
            }
            return messages.get(index).getText();
        }
        return null;
    }

    /**
     * 继续滚动
     *
     * @param delay 延迟指定时间后开始滚动
     */
    public void resume(long delay) {
        this.pause = false;
        if (mHandler.hasMessages(MARQUEE_MESSAGE)) {
            return;
        }

        mHandler.sendEmptyMessageDelayed(MARQUEE_MESSAGE, delay);
    }

    public void pause() {
        this.pause = true;
        mHandler.removeMessages(MARQUEE_MESSAGE);
    }

    public boolean isPause() {
        return pause;
    }

    private void continueMarquee() {
        if (scrollTop <= (-footerLine)) {
            scrollTop = 0;
            index++;
            pause();

            mHandler.sendEmptyMessageDelayed(NEXT_MESSAGE, 1500);
        } else {
            scrollTop -= distance;
        }
        invalidate();
    }

    public void setMessages(List<MessageText> messages) {
        this.messages = messages;
    }

    public List<MessageText> getMessages() {
        return messages;
    }

    public void setTextSize(float textSize) {
        this.textPaint.setTextSize(textSize);
        calculateFooter();
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        this.textPaint.setColor(textColor);
    }

    /**
     * 计算文字绘制底线
     */
    private void calculateFooter() {
        Paint.FontMetrics fm = textPaint.getFontMetrics();
        footerLine = (float) (realHeight * 1.0 / 2 - fm.descent + (fm.bottom - fm.top) / 2);
    }

}
